/**
 * Describe class Player here.
 *
 *
 * Created: Tue Mar 23 10:44:18 2010
 *
 * @author <a href="mailto:stg1@pc111-1.formation-continue.grenoble-inp.fr">stagiaire</a>
 * @version 1.0
 */

import java.io.*;

public final class MPlayer extends Thread{

    /**
     * Creates a new <code>Player</code> instance.
     *
     */
    BufferedOutputStream aos;
    String cmdSox;
    Piste pisteCour;
    boolean fin;
	private Process play;

    MPlayer() {
    }

    static byte[] echantillonToByte(int echcour, int resolution){
	int i= 0;
	byte[] res = new byte[resolution];
	while ( i < resolution){
	    res[i] = (byte)(echcour & 0xFF);
	    echcour = echcour >> 8;
	    i++;
	}
	return res;
    } 
    

    void play(Piste p){
	fin = false;
	cmdSox = String.format("play -r %d -c %1d %s -%1d -t raw -",
			    p.getFrequence(), 
			    p.getNbCanaux(),
			    p.getSigne() ? "-s" : "", 
			    p.getResolution());	

	try{
		/* Execute la commande Sox pour lire la musique
		 * et recupere un output stream pour ecrire des donnees
		 * vers le process de lecture.
		 */
	    this.play = Runtime.getRuntime().exec(cmdSox);
	    aos = new BufferedOutputStream(play.getOutputStream(),1000000);
	    pisteCour = p;
	    start();
	}catch(Exception e){
	    e.printStackTrace();
	    System.out.println(e);
	}

    }
    
    void pausePlayer(){
    	/* Envoie le code ASCII equivalent a CTRL+Z
    	 * dans le stream output du processus Sox
    	 * Writer writer = new OutputStreamWriter(aos);
    	try {
			writer.write("\032");
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
    	
    	/* Methode no2: stopper le process */
    
    	
    	
    	/*    	if(play != null) {
	    	play.destroy();
    	}
    }*/
    }
    /* 
     * Detache la piste du player et stoppe le process de lecture
     */
    void terminer(){
    	if(pisteCour != null && pisteCour.getPlayer() != null) {
    		pisteCour.retirerPlayer();
    		pausePlayer();
    	}
    	pisteCour = null;
    	this.fin = true;
    }

    public void run(){
	int nbech = pisteCour.getNbEch();
	int nbcan = pisteCour.getNbCanaux();
	int echcour;
	int canalcour = 0;
	byte [] buffer;
	int resolution = pisteCour.getResolution();
	System.out.println("start playing ...");
	pisteCour.initLecture();
	canalcour = 0;
	while (!pisteCour.finLecture() && !fin){
	    echcour = pisteCour.getCanal(canalcour).getEchantillon();
	    buffer = echantillonToByte(echcour,resolution );
	    try{
		aos.write(buffer, 0, resolution);
		
	    }catch(Exception e){
		e.printStackTrace();
		System.out.println(e);
	    }

	    canalcour = (canalcour + 1 ) % nbcan;
	}
	System.out.println("end reading");

    }

    public static void main(String[] args){
	Piste piste1 = null, piste2 = null;
	int s=0;
	piste1 = Piste.loadFromRawFile("dsquiban.raw",2, 16000, 2);
	piste2 = Piste.loadFromRawFile("lesEtoiles.raw",1, 16000, 2);
	do{
	    MPlayer mp = new MPlayer();
	    if (piste1 != null) mp.play(piste1);
	    MPlayer mp2 = new MPlayer();
	    if (piste2 != null) mp2.play(piste2);
	    try{ s = System.in.read();}catch(Exception e){}
	    mp.terminer();
	}while ( s != -1);
    }
}